<html>
<head><meta charset="utf-8"><title>containing `module_def_id` from span · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/containing.20.60module_def_id.60.20from.20span.html">containing `module_def_id` from span</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240764690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/containing%20%60module_def_id%60%20from%20span/near/240764690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/containing.20.60module_def_id.60.20from.20span.html#240764690">(May 30 2021 at 16:58)</a>:</h4>
<p>Hello everyone, is there a simple way to get the <code>module_def_id: LocalDefId</code> that a <code>span</code> resides in?</p>
<div class="spoiler-block"><div class="spoiler-header">
<p>Background:</p>
</div><div class="spoiler-content" aria-hidden="true">
<p>I'm trying to implement the <code>expect</code> attribute (RFC 2383). This requires the tracking of all emitted <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/struct.Diagnostic.html"><code>Diagnostics</code></a> which provide limited information about which module they are from. The best place to start is probably the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/span_encoding/struct.Span.html"><code>Span</code></a> which it contains. It would be good if I could from this get the <code>LocalDefId</code> of the module as this could be used to divide the check into several smaller task than checking the entire crate at once.</p>
</div></div>



<a name="240764772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/containing%20%60module_def_id%60%20from%20span/near/240764772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/containing.20.60module_def_id.60.20from.20span.html#240764772">(May 30 2021 at 17:00)</a>:</h4>
<p>I don't think so. The same span may be used within multiple modules. For example if you <code>include!()</code>, use <code>#[path = "..."] mod ...;</code> or use macros.</p>



<a name="240767843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/containing%20%60module_def_id%60%20from%20span/near/240767843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> xFrednet <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/containing.20.60module_def_id.60.20from.20span.html#240767843">(May 30 2021 at 18:22)</a>:</h4>
<p>Okay, thank you for the response! <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="240771827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/containing%20%60module_def_id%60%20from%20span/near/240771827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/containing.20.60module_def_id.60.20from.20span.html#240771827">(May 30 2021 at 20:11)</a>:</h4>
<p><span class="user-mention" data-user-id="369415">@xFrednet</span> To elaborate on what <span class="user-mention" data-user-id="133247">@bjorn3</span> said - there's very little meaningful information that you can extract from a <code>Span</code></p>



<a name="240771837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/containing%20%60module_def_id%60%20from%20span/near/240771837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/containing.20.60module_def_id.60.20from.20span.html#240771837">(May 30 2021 at 20:11)</a>:</h4>
<p>and the information that you <em>can</em> extract (the code snippet it points to,  the macro expansion it points to, etc) is usually only useful for diagnostics</p>



<a name="240771886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/containing%20%60module_def_id%60%20from%20span/near/240771886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/containing.20.60module_def_id.60.20from.20span.html#240771886">(May 30 2021 at 20:12)</a>:</h4>
<p>proc-macros have the ability to shuffle spans and <code>SyntaxContext</code>s around as they please, so you can't rely on them to tell you anything meaningful about where a piece of code originated</p>



<a name="240772430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/containing%20%60module_def_id%60%20from%20span/near/240772430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/containing.20.60module_def_id.60.20from.20span.html#240772430">(May 30 2021 at 20:26)</a>:</h4>
<p>The method for emitting lints doesn't directly take a span I believe. I think it is either a NodeId or DefId. You should take a look at the code handling lint levels. (<code>#[allow]</code>, <code>#[warn]</code>, ...)</p>



<a name="240776435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/containing%20%60module_def_id%60%20from%20span/near/240776435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/containing.20.60module_def_id.60.20from.20span.html#240776435">(May 30 2021 at 22:06)</a>:</h4>
<p>It is a HirId, I think to let you allow or deny the lint</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>